﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ZkSurvey.Common.SurveyMgr;
using Microsoft.Practices.EnterpriseLibrary.Data;
using System.Data.Common;
using System.Data;

namespace ZkSurvey.DAL.SurveyMgr
{
    public class AnswerDa
    {
        public static void InsertAnswer(Answer answer)
        {
            InsertAnswer(answer, DBHelper.GetDatabase(), null);
        }
        public static void InsertAnswer(Answer answer, TranscationHelper trans)
        {
            InsertAnswer(answer, trans.DataBase, trans.Transaction);
        }
        private static void InsertAnswer(Answer answer, Database db, DbTransaction trans)
        {
            object[] parameters = new object[]
            {
               answer.ParticipationId,
               answer.QuestionId,
               answer.Result,
               answer.UserInput 
            };

            try
            {
                if (trans == null)
                    answer.Id = Convert.ToInt32(db.ExecuteScalar("Usp_InsertAnswer", parameters));
                else
                    answer.Id = Convert.ToInt32(db.ExecuteScalar(trans, "Usp_InsertAnswer", parameters));
            }
            catch (System.Data.SqlClient.SqlException sex)      // 只捕获SqlException，其余抛出继续传播
            {
                DBHelper.ParseSqlException(sex, false);
            }
        }

        public static void DeleteAnswer(int id)
        {
            Database db = DBHelper.GetDatabase();

            try
            {
                db.ExecuteNonQuery("Usp_DeleteAnswer", id);
            }
            catch (System.Data.SqlClient.SqlException sex)      // 只捕获SqlException，其余抛出继续传播
            {
                DBHelper.ParseSqlException(sex, false);
            }
        }

        public static void UpateAnswer(Answer answer)
        {
            UpateAnswer(answer, DBHelper.GetDatabase(), null);
        }
        public static void UpateAnswer(Answer answer, TranscationHelper trans)
        {
            UpateAnswer(answer, trans.DataBase, trans.Transaction);
        }
        private static void UpateAnswer(Answer answer, Database db, DbTransaction trans)
        {
            object[] parameters = new object[]
            {
               answer.Id,
               answer.ParticipationId,
               answer.QuestionId,
               answer.Result,
               answer.UserInput 
            };

            try
            {
                if (trans == null)
                    db.ExecuteNonQuery("Usp_UpdateAnswer", parameters);
                else
                    db.ExecuteNonQuery(trans, "Usp_UpdateAnswer", parameters);
            }
            catch (System.Data.SqlClient.SqlException sex)      // 只捕获SqlException，其余抛出继续传播
            {
                DBHelper.ParseSqlException(sex, false);
            }
        }

        public static Answer GetAnswer(int id)
        {
            Database db = DBHelper.GetDatabase();
            Answer answer = null;

            using (IDataReader dataReader = db.ExecuteReader("Usp_GetAnswer", id))
            {
                if (dataReader.Read())
                {
                    answer = PopulateAnswer(dataReader);
                }
            }
            return answer;
        }
        public static List<Answer> GetAnswer(int? particId, int? quesId)
        {
            Database db = DBHelper.GetDatabase();
            List<Answer> answers = new List<Answer>();

            using (IDataReader dataReader = db.ExecuteReader("Usp_GetAnswerList", particId, quesId))
            {
                while (dataReader.Read())
                {
                    answers.Add(PopulateAnswer(dataReader));
                }
            }
            return answers;
        }

        private static Answer PopulateAnswer(IDataReader dataReader)
        {
            Answer answer = new Answer();
            answer.Id = dataReader.GetInt32(0);
            answer.ParticipationId = dataReader.GetInt32(1);
            answer.QuestionId = dataReader.GetInt32(2);
            if (!dataReader.IsDBNull(3))
                answer.Result = dataReader.GetString(3);
            if (!dataReader.IsDBNull(4))
                answer.UserInput = dataReader.GetString(4);

            return answer;
        }
    }
}
